package com.yylending.las.repay;

import android.text.TextUtils;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * P:借款详情 && 还款首页共用该接口
 * Created by laixj on 2017/5/25.
 * Copyright ©2016 www.yylending.com. All Rights Reserved.
 */
public class LoanDetailPresenter extends Presenter<LoanDetailModel, LoanDetailPresenter.View> {
	private final static String TAG = LoanDetailPresenter.class.getSimpleName();

	public static final String REPAY_STATUS_NO_DATA = "-1"; // 无借款数据
	public static final String REPAY_STATUS_NO_LOAN = "0"; // 无借款
	public static final String REPAY_STATUS_PAY_OFF = "1"; // 已还清
	public static final String REPAY_STATUS_HAS_PAY = "2"; // 已还款
	public static final String REPAY_STATUS_OVERDUE = "3"; // 逾期
	public static final String REPAY_STATUS_REPAY_PROCESSING = "4"; // 还款处理中
	public static final String REPAY_STATUS_TO_REPAY = "5"; // 待还

	public static class Generator implements PresenterFactory<LoanDetailPresenter> {
		@Override
		public LoanDetailPresenter create() {
			return new LoanDetailPresenter();
		}
	}

	public interface View extends IView {
		void getLoanDetailSuccess(List<LoanDetailModel.LoanDetail> result);

		void getLoanDetailFailed(String msg);
	}

	private View mView;
	private String mRequestPage;

	@Override
	public void attach(View view) {
		mRequestPage = view.getClass().getName();
		mView = view;
		mModel = new LoanDetailModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	/**
	 * 还款情况
	 */
	public void getLoanDetail(String loanNo, String loanType, boolean showTips) {
		LoanDetailModel.RequestParams params = new LoanDetailModel.RequestParams();
		params.loanNo = loanNo;
		params.type = loanType;
		execute(params, FUNCTIONAL_CODE, showTips, null, mRequestPage);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void loanDetailResult(LoanDetailModel.LoanDetailResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			if (BaseResultEntity.isResponseSuccess(result)) {
				List<LoanDetailModel.LoanDetail> loanDetail = result.getData();
				if (null != loanDetail) {
					mView.getLoanDetailSuccess(loanDetail);
				} else {
					mView.getLoanDetailFailed(TextUtils.isEmpty(result.getRetMsg()) ? "获取数据失败" : result.getRetMsg());
				}
			} else {
				mView.getLoanDetailFailed(TextUtils.isEmpty(result.getRetMsg()) ? "获取数据失败" : result.getRetMsg());
			}
		}
	}
}